/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Main.java
 *
 * Created on 6 oct. 2011, 11:46:52
 */
package Vue;

import Modele.ModeleVip;
import Metier.Vip;
import java.util.ArrayList;
import javax.swing.table.TableColumn;

/**
 * @author Babs
 */
public class Main extends javax.swing.JFrame {

    /** Creates new form Main */
    public Main() {
        initComponents();
        setLocation(250, 400);
        /* Gestion de la JTable */
        tableVip.setModel(new ModeleVip());
        tableVip.setColumnSelectionAllowed(false);
        tableVip.getTableHeader().setReorderingAllowed(false);
        this.monModele = (ModeleVip) tableVip.getModel();
        this.vipRepo = new Bdd.VipRepository();

        /* Gestion de la taille des collones */
        TableColumn column = null;
        for (int i = 0; i < 5; i++) {
            column = tableVip.getColumnModel().getColumn(i);
            switch (i) {
                case 0:
                    column.setPreferredWidth(35);
                    break;
                case 1:
                    column.setPreferredWidth(75);
                    break;
                case 2:
                    column.setPreferredWidth(75);
                    break;
                case 3:
                    column.setPreferredWidth(75);
                    break;
                case 4:
                    column.setPreferredWidth(75);
                    break;
                default:
                    column.setPreferredWidth(150);
                    break;
            }
        }


        setVisible(false);
        AdminConnect connection = new AdminConnect(this, true);
        connection.setLocationRelativeTo(this);
        connection.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tableVip = new javax.swing.JTable();
        Ltitle = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        btnCreateMariage = new javax.swing.JButton();
        btnCreateDivorce = new javax.swing.JButton();
        btnCreateVip = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        btnDisplayMariage = new javax.swing.JButton();
        btnDisplayDivorce = new javax.swing.JButton();
        btnDisplayVip = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        btnSelectionSupprimer = new javax.swing.JButton();
        btnNoSelection = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("VOilA - Vip Management");
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                formKeyReleased(evt);
            }
        });

        jScrollPane1.setViewportView(tableVip);

        Ltitle.setFont(new java.awt.Font("Aston-F1", 0, 24));
        Ltitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Ltitle.setText("vip management");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Adding data", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(153, 153, 153))); // NOI18N

        btnCreateMariage.setText("Add Wedding");
        btnCreateMariage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateMariageActionPerformed(evt);
            }
        });

        btnCreateDivorce.setText("Add Divorce");
        btnCreateDivorce.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateDivorceActionPerformed(evt);
            }
        });

        btnCreateVip.setText("Add VIP");
        btnCreateVip.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateVipActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCreateDivorce, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                    .addComponent(btnCreateMariage, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                    .addComponent(btnCreateVip, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(btnCreateMariage, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCreateDivorce, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCreateVip, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Display data", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(153, 153, 153))); // NOI18N

        btnDisplayMariage.setText("Weddings");
        btnDisplayMariage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDisplayMariageActionPerformed(evt);
            }
        });

        btnDisplayDivorce.setText("Divorces");
        btnDisplayDivorce.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDisplayDivorceActionPerformed(evt);
            }
        });

        btnDisplayVip.setText("VIP");
        btnDisplayVip.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDisplayVipActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnDisplayMariage, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                    .addComponent(btnDisplayDivorce, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                    .addComponent(btnDisplayVip, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(btnDisplayMariage, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnDisplayDivorce, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnDisplayVip, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnDisplayDivorce, btnDisplayMariage});

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Selection", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(153, 153, 153))); // NOI18N

        btnSelectionSupprimer.setText("Delete");
        btnSelectionSupprimer.setMaximumSize(new java.awt.Dimension(73, 23));
        btnSelectionSupprimer.setMinimumSize(new java.awt.Dimension(73, 23));
        btnSelectionSupprimer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectionSupprimerActionPerformed(evt);
            }
        });

        btnNoSelection.setText("Cancel selection");
        btnNoSelection.setMaximumSize(new java.awt.Dimension(73, 23));
        btnNoSelection.setMinimumSize(new java.awt.Dimension(73, 23));
        btnNoSelection.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNoSelectionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnSelectionSupprimer, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE)
                    .addComponent(btnNoSelection, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(btnSelectionSupprimer, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnNoSelection, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(71, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 695, Short.MAX_VALUE)
                    .addComponent(Ltitle, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 695, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Ltitle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCreateMariageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateMariageActionPerformed
        Mariage mariage = new Mariage(this, true);
        mariage.setVisible(true);
        setLocationRelativeTo(this);
        //Retour de la boite de dialogue Mariage
        tableVip.setModel(new Modele.ModeleMariage());
        displayWeddings();
}//GEN-LAST:event_btnCreateMariageActionPerformed

    private void btnCreateDivorceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateDivorceActionPerformed
        Divorce divorce = new Divorce(this, true);
        divorce.setVisible(true);
        setLocationRelativeTo(this);
        //Retour de la boite de dialogue Divorce
        tableVip.setModel(new Modele.ModeleDivorce());
        displayDivorces();
    }//GEN-LAST:event_btnCreateDivorceActionPerformed

    private void formKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyReleased
         if(evt.getKeyCode() == java.awt.event.KeyEvent.VK_ESCAPE) {
            System.exit(0);
        }
    }//GEN-LAST:event_formKeyReleased

    private void btnNoSelectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNoSelectionActionPerformed
        tableVip.clearSelection();
    }//GEN-LAST:event_btnNoSelectionActionPerformed

    private void btnDisplayMariageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDisplayMariageActionPerformed
        tableVip.setModel(new Modele.ModeleMariage());
        displayWeddings();
    }//GEN-LAST:event_btnDisplayMariageActionPerformed

    private void btnDisplayDivorceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDisplayDivorceActionPerformed
        tableVip.setModel(new Modele.ModeleDivorce());
        displayDivorces();
    }//GEN-LAST:event_btnDisplayDivorceActionPerformed

    private void btnDisplayVipActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDisplayVipActionPerformed
        tableVip.setModel(new ModeleVip());
        displayVip();
    }//GEN-LAST:event_btnDisplayVipActionPerformed

    private void btnCreateVipActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateVipActionPerformed
        Vue.Vip vip = new Vue.Vip(this, true);
        vip.setVisible(true);
        setLocationRelativeTo(this);
        //Retour de la boite de dialogue Divorce
        tableVip.setModel(new Modele.ModeleVip());
        displayVip();
    }//GEN-LAST:event_btnCreateVipActionPerformed

private void btnSelectionSupprimerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectionSupprimerActionPerformed
    int[] lesLignes = tableVip.getSelectedRows();
    ((Modele.Modele)tableVip.getModel()).suppLignes(lesLignes);
}//GEN-LAST:event_btnSelectionSupprimerActionPerformed

    void displayDivorces()
    {
         tableVip.setColumnSelectionAllowed(false);
        tableVip.getTableHeader().setReorderingAllowed(false);
        this.modeleDivorce = (Modele.ModeleDivorce) tableVip.getModel();
        this.divorceRepo = new Bdd.DivorceRepository();
        System.out.println("Chargement des divorces");
        ArrayList<Metier.Divorce> list = divorceRepo.loadAll();
        modeleDivorce.showData(list);
        Ltitle.setText("Divorces");
    }

    void displayWeddings()
    {
        this.mariageRepo = new Bdd.MariageRepository();
        ArrayList<Metier.Mariage> list = mariageRepo.loadAll();
        if(!list.isEmpty()) {
            tableVip.setColumnSelectionAllowed(false);
            tableVip.getTableHeader().setReorderingAllowed(false);
            this.modeleMariage = (Modele.ModeleMariage) tableVip.getModel();
            modeleMariage.showData(list);
            Ltitle.setText("Weddings");
        }
        else {
            javax.swing.JOptionPane.showMessageDialog(this, "Aucun mariage disponible", "Erreur chargement", javax.swing.JOptionPane.ERROR_MESSAGE);
        }
    }

    void displayVip()
    {
        tableVip.setColumnSelectionAllowed(false);
        tableVip.getTableHeader().setReorderingAllowed(false);
        this.monModele = (ModeleVip) tableVip.getModel();
        this.vipRepo = new Bdd.VipRepository();
        System.out.println("Chargement des Vip");
        ArrayList<Vip> list = vipRepo.loadAll();
        monModele.showData(list);
        Ltitle.setText("Vip");
    }
    /* class MonEcouteurTable implements javax.swing.event.TableModelListener {

    public void tableChanged(javax.swing.event.TableModelEvent e) {
    if (e.getType() == javax.swing.event.TableModelEvent.INSERT) {
    System.out.println("insertion ligne " + (e.getFirstRow() + 1));
    } else if (e.getType() == javax.swing.event.TableModelEvent.DELETE) {
    System.out.println("suppression ligne " + (e.getFirstRow() + 1) + " à " + (e.getLastRow() + 1));
    } else if (e.getType() == javax.swing.event.TableModelEvent.UPDATE) {
    int l = e.getLastRow();
    int c = e.getColumn();
    javax.swing.table.TableModel model = (javax.swing.table.TableModel) e.getSource();
    String nom = model.getColumnName(c);
    Object data = model.getValueAt(l, c);
    System.out.println("Modification : ");
    System.out.println("Ligne : " + (l + 1) + " Colonne : " + (c + 1));
    System.out.println("  Nom colonne : " + nom);
    System.out.println("  Contenu cellule : " + data);
    System.out.println("--------------------------");
    }
    }
    }*/
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    new Main().setVisible(true);
                } catch (Exception e) {
                    System.out.println("Exception look : " + e);
                    new Main().setVisible(true);
                }
            }
        });
    }

    public void startApp() {
        System.out.println("Demarrage de l'application");
        this.setVisible(true);
        System.out.println("Chargement des Vip");
        ArrayList<Vip> list = vipRepo.loadAll();
        monModele.showData(list);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Ltitle;
    private javax.swing.JButton btnCreateDivorce;
    private javax.swing.JButton btnCreateMariage;
    private javax.swing.JButton btnCreateVip;
    private javax.swing.JButton btnDisplayDivorce;
    private javax.swing.JButton btnDisplayMariage;
    private javax.swing.JButton btnDisplayVip;
    private javax.swing.JButton btnNoSelection;
    private javax.swing.JButton btnSelectionSupprimer;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableVip;
    // End of variables declaration//GEN-END:variables

    private Bdd.VipRepository vipRepo;
    private Bdd.MariageRepository mariageRepo;
    private Bdd.DivorceRepository divorceRepo;
    private ModeleVip monModele;
    private Modele.ModeleMariage modeleMariage;
    private Modele.ModeleDivorce modeleDivorce;
}
